
$(document).ready(function(){	
	// ddd localStorage.removeItem('auth');
	
	console.log("[DEBUG] localStorage['auth'] "+localStorage['auth']);
	QB.init(QBPARAMS.app_id, QBPARAMS.auth_key, QBPARAMS.auth_secret);

	sessionCreate(STEP_INIT_APP);
    
	$( "#addNewUserButton" ).click(function() {
		  console.log("CHECK ALL USER DETAILS");
		  if(checkAddNewUserForm()){
		  	$("#signUpUserPage").spin("modal");
		     addNewUser();  
		  }		   
	});
		 
});


function sessionCreate(step) {  	
	console.log(" SessionCreate Starts ");		
	if (localStorage['auth']) {
	  	$("#signUpUserPage").spin("modal");
		console.log("sessionCreate storage tiene valor");		
		storage = JSON.parse($.base64.decode(localStorage['auth']));	
		console.log("createSession fue bien. "+JSON.stringify(storage)); 
		loginQBUser(storage.email,storage.password,storage.scringoId,step);
	}	
}

function checkAddNewUserForm(){
	//email, login, status, passwo
	console.log("+"+$("#email").val()+"-");
	if($("#email").val() == ''){
		//Show alert
		showOneButtonPopupDialog('Error','E-mail is Mandatory.');
		return false;
	}else{
		if(!validEmail($("#email").val())){
			showOneButtonPopupDialog('Error','E-mail format not valid.');
			return false;
		}
	}
	
	 
	if($("#status").val() == ''){
		//Show alert
		showOneButtonPopupDialog('Error','Status is Mandatory.');
		return false;
	}
	
	if($("#password").val() == ''){
		//Show alert
		showOneButtonPopupDialog('Error','Password is Mandatory.');
		return false;
	}
	 return true;
}

function addNewUser(){	
	registerScringoUser($("#email").val() ,$("#password").val(),$("#email").val().split('@')[0] , STEP_REGISTER_USER );
//	registerScringoUser('rocamorarubio123@gmail.com' ,'123456789','rocamorarubio123', STEP_REGISTER_USER );	

}